/*
 * Copyright (C) 2020-2021 HERE Europe B.V.
 * Licensed under Apache 2.0, see full license in LICENSE
 * SPDX-License-Identifier: Apache-2.0
 */

import { FlatTheme, ImageTexture, Light, Style } from "@here/harp-datasource-protocol";

//    Mocha discourages using arrow functions, see https://mochajs.org/#arrow-functions

/**
 * Utility Class for Rendering IBCT Tests
 */
export class ThemeBuilder {
    static readonly imageTextures: ImageTexture[] = [
        {
            name: "white-icon",
            image: "white-icon"
        },
        {
            name: "red-icon",
            image: "red-icon"
        },
        {
            name: "green-icon",
            image: "green-icon"
        }
    ];

    static readonly lights: Light[] = [
        {
            type: "ambient",
            color: "#FFFFFF",
            name: "ambientLight",
            intensity: 0.9
        },
        {
            type: "directional",
            color: "#FFFFFF",
            name: "light1",
            intensity: 0.8,
            direction: {
                x: 1,
                y: 5,
                z: 0.5
            }
        }
    ];

    static readonly markerStyle: Style = {
        id: "baseOrder",
        when: ["==", ["geometry-type"], "Point"],
        technique: "labeled-icon",

        //        imageTexture: "white-icon",
        size: ["number", ["get", "size"], 15],
        iconYOffset: ["number", ["get", "iconYOffset"], 30],
        text: ["get", "text"],
        color: ["get", "color"],
        imageTexture: ["string", ["get", "imageTexture"], "white-icon"],
        renderOrder: ["get", "renderOrder"],
        textReserveSpace: false,
        iconReserveSpace: false,
        styleSet: "geojson"
    };

    static readonly images = {
        "red-icon": {
            // tslint:disable-next-line:max-line-length
            url:
                "",
            preload: true
        },
        "green-icon": {
            // tslint:disable-next-line:max-line-length
            url:
                "",
            preload: true
        },
        "white-icon": {
            // tslint:disable-next-line:max-line-length
            url:
                "",
            preload: true
        },
        "plus-icon": {
            url:
                "",
            preload: true
        }
    };

    private readonly m_baseTheme: FlatTheme = {
        lights: ThemeBuilder.lights,
        sky: {
            type: "gradient",
            topColor: "#161719",
            bottomColor: "#262829",
            groundColor: "#262829"
        },
        clearColor: "#4A4D4E",
        images: ThemeBuilder.images,
        imageTextures: ThemeBuilder.imageTextures,
        styles: []
    };

    private m_theme: FlatTheme = { styles: [] };

    /**
     *
     * @param m_useEmptyTheme - If `true` initializes with an empty Theme, @default `false`
     *  The default uses some basic theme settings for initialization.
     */
    constructor(private readonly m_useEmptyTheme: boolean = false) {
        if (!this.m_useEmptyTheme) {
            this.m_theme = this.m_baseTheme;
        }
    }

    build(): FlatTheme {
        return this.m_theme;
    }

    withFontCatalog(): ThemeBuilder {
        this.m_theme = {
            ...this.m_theme,
            fontCatalogs: [
                {
                    name: "fira",
                    url: "../dist/resources/fonts/Default_FontCatalog.json"
                }
            ]
        };
        return this;
    }

    withInvalidFontCatalog(): ThemeBuilder {
        this.m_theme = {
            ...this.m_theme,
            fontCatalogs: [
                {
                    name: "invalid",
                    url: "invalid.json"
                }
            ]
        };
        return this;
    }

    withMarkerStyle(): ThemeBuilder {
        return this.withStyle(ThemeBuilder.markerStyle);
    }

    withStyle(style: Style): ThemeBuilder {
        this.m_theme.styles!.push(style);
        return this;
    }
}
